class SessionStorageUtil {
  /**
   * 设置 sessionStorage 项
   * @param key 键
   * @param value 值
   */
  static setItem(key: string, value: any): void {
    try {
      const serializedValue = JSON.stringify(value)
      sessionStorage.setItem(key, serializedValue)
    } catch (error) {
      console.error('Error setting sessionStorage item:', error)
    }
  }

  /**
   * 获取 sessionStorage 项
   * @param key 键
   * @returns 值
   */
  static getItem<T>(key: string): T | null {
    try {
      const serializedValue = sessionStorage.getItem(key)
      return serializedValue ? JSON.parse(serializedValue) : null
    } catch (error) {
      console.error('Error getting sessionStorage item:', error)
      return null
    }
  }

  /**
   * 删除 sessionStorage 项
   * @param key 键
   */
  static removeItem(key: string): void {
    try {
      sessionStorage.removeItem(key)
    } catch (error) {
      console.error('Error removing sessionStorage item:', error)
    }
  }

  /**
   * 清空所有 sessionStorage 项
   */
  static clear(): void {
    try {
      sessionStorage.clear()
    } catch (error) {
      console.error('Error clearing sessionStorage:', error)
    }
  }
}

export default SessionStorageUtil